import React, { Component, Fragment } from "react";
// 
import { Button } from "antd";
// antd
import Action from "@c/buttonAction/Index";
// table 组件
import TableComponent from "@c/tableData/Index";
import ServiceModal from "./components/ServiceModal";
// Store
import Store from "@/stroe/Index";
// action
import { addStatus } from "@/stroe/action/Config";
import "./index.scss";

class ServiceList extends Component {
    constructor(props){
        super(props);
        this.state = {
            // id
            id: "",
            // 表头
            tableConfig: {
                url: "serviceList",
                method: "get",
                checkbox: true,
                thead: [
                    { title: "", dataIndex: "index_id", key: "index_id" },
                    { title: "名称", dataIndex: "name", key: "name" },
                    { title: "代号", dataIndex: "code", key: "code" },
                    { 
                        title: "管理", 
                        dataIndex: "manage", 
                        key: "manage", 
                        width: 215,
                        render: (text, rowData) => {
                            return (
                                <div className="inline-button">
                                    <Action>
                                        <Action.Button onClick={() => this.serviceModal({status: true, service_id: rowData.id})} disabled>编辑</Action.Button>
                                        <Action.Button onClick={() => this.delete(rowData.id)}>删除</Action.Button>
                                    </Action>
                                </div>
                            )
                        }
                    }
                ],
                formItem: [
                    { 
                        type: "Input",
                        label: "服务名称", 
                        name: "name", 
                        placeholder: "请输入服务名称"
                    },
                    { 
                        type: "Input",
                        label: "服务代号", 
                        name: "code", 
                        placeholder: "请输入服务代号"
                    }
                ]
            },
            // 表的数据
            data: []
        };
    }
    /** 生命周期挂载完成 */
    componentDidMount(){
        Store.subscribe(() =>
            console.log(Store.getState())
        );

        Store.dispatch(addStatus({
            label: "Centos7",
            value: "centos7"
        }))

        // Store.dispatch(uploadStatus("aaaaa", false))
    }
    // 获取子组件实例
    getChildRef = (ref) => {
        this.tableComponent = ref; // 存储子组件
    }

    // 获取弹窗子组件实例
    getServiceModalRef = (ref) => { this.child = ref }
    // 显示弹窗
    serviceModal = (data) => {
        console.log(data)
        this.child.visibleModal(data);
    }
    /** 删除 */
    delete = (id) => {
        this.tableComponent.onHandlerDelete(id)
    }
    
    render(){
        return (
            <Fragment>
                <TableComponent onRef={this.getChildRef} batchButton={true} config={this.state.tableConfig} >
                    <Button type="primary" ref="serviceAdd" onClick={() => this.serviceModal({ status: true })}>创建服务</Button>
                </TableComponent>
                <ServiceModal onRef={this.getServiceModalRef}/>
            </Fragment>
        )
    }
}

export default ServiceList